package com.astroway.livetest.presenters;

import android.util.Log;


import com.astroway.livetest.model.CurLiveInfo;
import com.astroway.livetest.model.MemberID;
import com.astroway.livetest.model.MySelfInfo;
import com.astroway.livetest.model.RecordInfo;
import com.astroway.livetest.model.RoomInfoJson;
import com.astroway.livetest.util.Constants;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tencent.ilivesdk.core.ILiveLog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static android.R.attr.data;


/**
 * 网络请求类
 */
public class UserServerHelper {
    private static final String TAG = UserServerHelper.class.getSimpleName();
    private static UserServerHelper instance = null;
    private static final String IP = "https://sxb.qcloud.com/sxb/index.php?";
    public static final String REGISTER = IP + "svc=account&cmd=regist";
    public static final String LOGIN = IP + "slogivc=account&cmd=n";
    public static final String LOGOUT = IP + "svc=account&cmd=logout";
    public static final String APPLY_CREATE_ROOM = IP + "svc=live&cmd=create";
    public static final String REPORT_ROOM_INFO = IP + "svc=live&cmd=reportroom";
    public static final String HEART_BEAT = IP + " svc=live&cmd=heartbeat";
    public static final String STOP_ILIVE = IP + "svc=live&cmd=exitroom";
    public static final String GET_ROOMLIST = IP + "svc=live&cmd=roomlist";
    public static final String REPORT_ME = IP + "svc=live&cmd=reportmemid";
    public static final String GET_MEMLIST = IP + "svc=live&cmd=roomidlist";
    public static final String REPORT_RECORD = IP + "svc=live&cmd=reportrecord";
    public static final String GET_REOCORDLIST = IP + "svc=live&cmd=recordlist";
    public static final String GET_PLAYERLIST = IP + "svc=live&cmd=livestreamlist";
    public static final String GET_ROOM_PLAYURL = IP + "svc=live&cmd=getroomplayurl";
    public static final String GET_COS_SIG = IP + "svc=cos&cmd=get_sign";


    private String token = ""; //后续使用唯一标示
    private String Sig = ""; //登录唯一标示
    //    private int avRoom;
    //    private String groupID;

    public class RequestBackInfo {

        int errorCode;
        String errorInfo;

        RequestBackInfo(int code, String bad) {
            errorCode = code;
            errorInfo = bad;
        }

        public int getErrorCode() {
            return errorCode;
        }


        public String getErrorInfo() {
            return errorInfo;
        }

    }


    public static UserServerHelper getInstance() {
        if (instance == null) {
            instance = new UserServerHelper();
        }
        return instance;
    }


    private ArrayList<RoomInfoJson> roomList;

    public ArrayList<RoomInfoJson> getRoomListData() {
        return roomList;
    }


    public static final MediaType JSON
            = MediaType.parse("application/json; charset=utf-8");

    private OkHttpClient client = new OkHttpClient.Builder()
            .connectTimeout(5, TimeUnit.SECONDS)
            .readTimeout(5, TimeUnit.SECONDS)
            .build();


    public String post(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            return response.body().string();
        } else {
            return "";
        }
    }


    /**
     * 注册ID （独立方式）
     */
    public RequestBackInfo registerId(String id, String password) {
        try {
            JSONObject jasonPacket = new JSONObject();
            jasonPacket.put("id", id);
            jasonPacket.put("pwd", password);
            String json = jasonPacket.toString();
            String res = post(REGISTER, json);
            JSONTokener jsonParser = new JSONTokener(res);
            JSONObject response = (JSONObject) jsonParser.nextValue();
            int code = response.getInt("errorCode");
            String errorInfo = response.getString("errorInfo");

            return new RequestBackInfo(code, errorInfo);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 登录ID （独立方式）
     */
    public RequestBackInfo loginId(String id, String password) {
        try {
            JSONObject jasonPacket = new JSONObject();
            jasonPacket.put("id", id);
            jasonPacket.put("pwd", password);
            //            String json = jasonPacket.toString();
            //            String res = post(LOGIN, json);
            //            JSONTokener jsonParser = new JSONTokener(res);
            //            JSONObject response = (JSONObject) jsonParser.nextValue();
            //
            //            int code = response.getInt("errorCode");
            //            String errorInfo = response.getString("errorInfo");
            //            if (code == 0) {
            //                JSONObject data = response.getJSONObject("data");


            Sig = getSig(id);
            MySelfInfo.getInstance().setId(id);
            MySelfInfo.getInstance().setUserSig(Sig);
            MySelfInfo.getInstance().setToken(token);

            //            }
            return new RequestBackInfo(0, "succeed");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    private String getSig(String phoneNum) {
        String sig = "";
        if (phoneNum.equals("15689523198")) {
            sig = "eJxlzkFPgzAUwPE7n4L0OuPa0go12aF1UydbiDqTuQtBaLeKQgeFocbvrqJREt-1-8t7781xXResFrfHSZqWTWFj*2IkcE9dAMHRXzRGZ3FiY6-K" +
                    "-kXZGV3JOFFWVn1ElFIM4dDoTBZWK-0rTgJGsYdYMEB1lsf9pW9DPld4EBF" +
                    "-SPS2j8vZ9dmc51G622wezZOB2IyVqFlzsW7ZgXf7uxDXN7utbg8kT6HgWiyDaSIIf5hHl4UtZuPo6ny0aNaNGhH8et*Fvkr30SoU05JPJoOTVj" +
                    "-Ln4cYwZj6HhnUVla1LoseYIgowh78GuC8Ox8sJ10*";
        } else if (phoneNum.equals("13658692821")) {
            sig = "eJxlzl1PgzAUgOF7fkXDLcaUQhmYeLFPgkIy56obNw3SjnRkpUIVFuN-V1FjE8-t**Sc82YBAOxten9ZlGXzIjXVZ8VtcAVsaF-8RaUEo4WmXsv*RT4o0XJaHDRvx" +
                    "*hijBGEphGMSy0O4ld4AQ6DCIXINVDHajpe*jb*5woPuv7EJKIaY7Yk82TuZIt9jsrJJkRVPuQ1CfdFdZs6LFvIkIie" +
                    "*NsZ3PVsCKZiOT3erXeJc3rcyJiviEKwWd3oZxgnMkjirn5Y69Q5zrz*KSLXxkktTvznochHCEcwMuorbzvRyBEg6GIXefBrbOvd*gCBtlw2";
        } else if (phoneNum.equals("13105121116")) {
            sig = "eJxlj01PwkAURff9Fc1sMWY*QUxYgFaKVgRLGspmUtqhPrWlTqfEavzvYtU4iW97Tu69791xXRetgvA0SdN9Uxpp2koh99xFGJ38waqCTCZGMp39g" +
                    "*q1Aq1ksjNKd5AIISjGtgOZKg3s4NdgBAtCCSF9S6qzJ9k1fTv8GMEw4QNbgbyDt97yYjZ923p427YPxXQOvfo*SC" +
                    "-HPI7Cgy42zRkzURGtibgbrGbZGCZpWKvqhT2KAPe9m*bqmkz0gqfgL-x22Xs2YUzbdexv8nk*GlmVBgr1M2jIKRVDKix6ULqGfdkJFJPjUwx-HXI*nE-50V0V";
        } else if (phoneNum.equals("17685757560")) {
            sig = "eJxljl1PwjAUhu-3K5rdYqRrV7aZeIEFZQEVosyPm2Zs3XICrHV0Y2D87*LUuMRzLp8n7-u*Wwgh" +
                    "*3H2cB4niaoKI8xBSxtdIBvbZ39Qa0hFbAQt039QNhpKKeLMyLKFDmOMYNx1IJWFgQx*DW-gM" +
                    "*-0g660S9eibfp23FMExY7rdRXIW3g7XvJwwUccH2c3x15tws2Bj14jUAae4wKG0TZRT4v7K7Oez5Wu0jzM7" +
                    "*SEV831yqN03HvZv4VZyadREtDGz5ebaTDpE8ZWw1r2-ctOpYGt-BkUuISwgHYH1bLcgSpagWCHOYTir7OtD*sTZAldmw__";
        } else if (phoneNum.equals("18266427095")) {
            sig = "eJxlj11PgzAUhu-5FU1vMaYtLVu9Mxsg*8BsbnHzhhAo29FYalfNmPG-q0wjief2efK" +
                    "*73n3EEJ4Nbu7LMqyedUud61RGF0hTPDFHzQGqrxweWCrf1AdDViVF7VTtoNUCMEI6TtQKe2ghl9jyMKQswGRoicdqqe8azo7-CsiIJQP" +
                    "*grsOjiPtqN0Md5MyORRJLosWALmVLXtkkK2fdlwJ*MAXKYzK*c2StNFur*e7ulaxQ*z6KZu3HDth8m09W8Zv5exv0xH*hjJLBzvpAnLXqWDZ" +
                    "-UzSHLGhBT9x96UPUCjO4ERKigLyPdh78P7BKc7XBg_";
        } else if (phoneNum.equals("13553067526")) {
            sig = "eJxlzltPgzAUwPF3PgXp64zrhbJi4ssQAkY675GnhkDBhnHvNpfF766ixiae1-8v55yTZds2eLx5OM" +
                    "-yvNu1WuhjL4F9YQMIzv5i36tCZFqQsfgX5VuvRimyUstxjohSiiE0jSpkq1WpfgWhlEB3RbFroKmoxXzp2zifKwhEzsokqppjEjz58Z2fTiXk6" +
                    "*GVRFnedOXmuA6SDRu8*4LxJIWHkC95rW5bvDjEVRDV7mJ8QVeBftZ*CCPMmjjkfM9reZ1u5RB2jGyX1Y6zS*OkVo38echzMKaei426l" +
                    "*OkunYGGCKKMIFfA6x36wOoEVxl";
        }
        return sig;
    }

    /**
     * @date 创建时间: 2017/5/8
     * @author XiaoFeng
     * @Description 老版sig签名
     */
    //    private String getSig(String phoneNum) {
    //        String sig = "";
    //        if (phoneNum.equals("15689523198")) {
    //            sig = "eJxlzsFOg0AQgOE7T0G4anR3YQuY9KBIUIvBYg2Fywa6C50U6ZauFGt8dxWNkjjX-8vMvGm6rhuL8PEsX622L41i6lUKQ7-QDWSc-kUpgbNcMbPl" +
    //                    "-6LoJbSC5aUS7RAxpZQgNDbARaOghF8xcVxKTOw6I7TnGzZc*jbW5wpiUZeMCVRDvPfn3q33dFLHuxsfrsNiF3JrZsVJ4CR3Udwf3UliR4FzXPCyWy" +
    //                    "-lJVzVZVpV0ezwAI21trMu6WUdCRz6GT33zKwOqoRvimWaFvPpdHRSwbP4echFyHYIske1E*0ets0ACMIUExN9jaG9ax8Z5F19";
    //        } else if (phoneNum.equals("13658692821")) {
    //            sig = "eJxlj1FPgzAUhd-5FYRXjbaFTjDxAaYiRmY6Jca9EKQFS9fCoGwO43*fokYS7*v35Zxz3w3TNK3Hu4eTLM-rXulU7xtmmeemBazjP9g0nKaZTu2W" +
    //                    "-oPsreEtS7NCs3aEEGOMAJg6nDKlecF-DXuG3ZmHXAQnUkdFOjZ9O85nBHKwh6YKL0cYXyXziMyXIBhE362GpHx2dvvNDQgZ7fNVzBbemdhxQpUcIF" +
    //                    "-HikTl5UvYsC3pkldxi3RQlNfr*66Sp-6TFHlVh0r6m*AoSiqfXEwqNZfsZ5DrYQxde-rYlrUdr9UoIAAxRDb4Osv4MA4Q-V9r";
    //        } else if (phoneNum.equals("13105121116")) {
    //            sig = "eJxlj1FPgzAUhd-5FQ2vGtdb6BwmPjBWjZkiy9wYTwRpt1wd0LCqQ*N-d0ONTbyv35dzzv1wCCHuw*38rCjL5qU2uem0cskFcal7*ge1RpkXJvda*Q" +
    //                    "*qvcZW5cXaqLaHwDlnlNoOSlUbXOOv4QHlwABgaEk7*Zz3Td*Of4hgPg*YreCmh3diEd2Mp6us3XbRMLg3Sw4D81b5tJHVJF6GodCGTmD*9M4HgbcJUYSBKK" +
    //                    "-VOGOLboqQprVOE5ZsWRKnKy2uqHwUs-OTJoopZpdWpcFK-QwaBZzDCOxBr6rdYVP3AqNweMqjx3OdT*cLSpRblQ__";
    //        } else if (phoneNum.equals("17685757560")) {
    //            sig = "eJxljl1PgzAYhe-5FU1vMa6UFcTEC2KasfkRo9vYuCFAO1bJGJaWKMb-PoYam3jey*fJOe*nBQCAy-uXy6wojrpWqfpoOATXACJ48QebRrA0U6kr2T" +
    //                    "-I3xsheZrtFJcjdAghGCHTEYzXSuzEr*F7V8QfzjOlllXpuPTtTIcKPCUBNhVRjvCBrm7nIenLLqfBs3Nn*9VKsH2sez3xk6X7lkdPyWtDotk" +
    //                    "*wo8HHQoa1us1JUVvY8YWJbXLxSyuCKu6nifz7SYO9CTb5m2NN7K6MSaVOPCfhwLkeO4Qg3ZctuJYjwJGDnGwi86B1pd1Ah8TXY0_";
    //        } else if (phoneNum.equals("18266427095")) {
    //            sig = "eJxlzk1Pg0AQgOE7v4LstcbMfrZ4Q0Sz9au0BFsvBGHBtZYCXWgb439XUSOJc32fzMybZds2Cm8Wp0mabtvSxOZYKWSf2QjQyV*sKp3FiYlpk" +
    //                    "-2L6lDpRsVJblTTR8w5JwBDozNVGp3rXzEhQjAyBocP0C5bx-2lb8M*VxDGHTIkuujjrb-yZOD50zvgLyGs6tkyvIjkkprI4wq7hynsI13Xe-Hk
    // *Vk7Sgv57Er" +
    //                    "-oSsUnT2OF0yOjuYqzM9hcuk6m9d7VVy3FIJgvm5pB8HgpNEb9fOQA1hQJtigdqrZ6W3ZAwKYY0Lha5D1bn0AiYRcOA__";
    //        } else if (phoneNum.equals("13553067526")) {
    //            sig = "eJxlzk1Pg0AQgOE7v2LDVaP7wWIw8WI-YhVTWlDBy4awA64Edgtb22r87ypqJHGu75OZeXMQQm4Sxid5Uehta4U9GHDROXKxe" +
    //                    "-wXjVFS5FawTv6LsDeqA5GXFrohEs45xXhslITWqlL9CsY5w-4Zp" +
    //                    "-4I9bIWw6Vv432uoB4P6Jioaoi3s7vJYjXFeq3TPrzfn2LdMGWiLJmYCFZ51IRTMof1roJDEDzv8mpRPdxkmS3sY7pJji69q
    // -I6qzd0O3uN6ZzEHnsyfpcul30to" +
    //                    "*pidNKqBn4eCjDxGSdsVF*g65VuB0Ax4YQy-DWu8*58AGHeXfk_";
    //        }
    //        return sig;
    //    }

    /**
     * 登出ID （独立方式）
     */
    public RequestBackInfo logoutId(String id) {
        try {
            JSONObject jasonPacket = new JSONObject();
            jasonPacket.put("id", id);
            jasonPacket.put("token", MySelfInfo.getInstance().getToken());
            String json = jasonPacket.toString();
            String res = post(LOGOUT, json);
            JSONTokener jsonParser = new JSONTokener(res);
            JSONObject response = (JSONObject) jsonParser.nextValue();

            int code = response.getInt("errorCode");
            String errorInfo = response.getString("errorInfo");
            return new RequestBackInfo(code, errorInfo);

        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 申请创建房间
     */
    public RequestBackInfo applyCreateRoom() {
        try {
            JSONObject jasonPacket = new JSONObject();
            jasonPacket.put("type", "live");
            jasonPacket.put("token", MySelfInfo.getInstance().getToken());
            String json = jasonPacket.toString();
            String res = post(APPLY_CREATE_ROOM, json);
            JSONTokener jsonParser = new JSONTokener(res);
            JSONObject response = (JSONObject) jsonParser.nextValue();
            int code = response.getInt("errorCode");
            String errorInfo = response.getString("errorInfo");
            if (code == 0) {
                JSONObject data = response.getJSONObject("data");
                int avRoom = data.getInt("roomnum");
                MySelfInfo.getInstance().setMyRoomNum(avRoom);
                CurLiveInfo.setRoomNum(avRoom);
                String groupID = data.getString("groupid");
            }
            return new RequestBackInfo(code, errorInfo);

        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 上报房间信息
     */
    public RequestBackInfo reporNewtRoomInfo(String inputJson) {
        try {

            String res = post(REPORT_ROOM_INFO, inputJson);
            JSONTokener jsonParser = new JSONTokener(res);
            JSONObject response = (JSONObject) jsonParser.nextValue();
            int code = response.getInt("errorCode");
            String errorInfo = response.getString("errorInfo");
            return new RequestBackInfo(code, errorInfo);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /***
     * 上报录制信息
     */
    public RequestBackInfo reporNewtRecordInfo(String inputJson) {
        try {
            Log.v(TAG, "reporNewtRecordInfo->" + inputJson);
            String res = post(REPORT_RECORD, inputJson);
            JSONTokener jsonParser = new JSONTokener(res);
            JSONObject response = (JSONObject) jsonParser.nextValue();
            int code = response.getInt("errorCode");
            String errorInfo = response.getString("errorInfo");
            RequestBackInfo ret = new RequestBackInfo(code, errorInfo);
            Log.v(TAG, "reporNewtRecordInfo->rsp:" + ret.errorCode + "|" + ret.getErrorInfo());
            return ret;
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 心跳上报
     */
    public RequestBackInfo heartBeater(int role) {
        try {
            JSONObject jasonPacket = new JSONObject();
            jasonPacket.put("role", role);
            jasonPacket.put("token", MySelfInfo.getInstance().getToken());
            jasonPacket.put("roomnum", MySelfInfo.getInstance().getMyRoomNum());
            jasonPacket.put("thumbup", CurLiveInfo.getAdmires());
            String json = jasonPacket.toString();
            String res = post(HEART_BEAT, json);
            JSONTokener jsonParser = new JSONTokener(res);
            JSONObject response = (JSONObject) jsonParser.nextValue();
            int code = response.getInt("errorCode");
            String errorInfo = response.getString("errorInfo");
            return new RequestBackInfo(code, errorInfo);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 获取房间列表
     */
    public RequestBackInfo getRoomList() {
        try {
            JSONObject jasonPacket = new JSONObject();
            jasonPacket.put("token", MySelfInfo.getInstance().getToken());
            jasonPacket.put("type", "live");
            jasonPacket.put("index", 0);
            jasonPacket.put("size", 20);
            jasonPacket.put("appid", Constants.SDK_APPID);
            String json = jasonPacket.toString();
            String res = post(GET_ROOMLIST, json);
            JSONTokener jsonParser = new JSONTokener(res);
            JSONObject response = (JSONObject) jsonParser.nextValue();
            int code = response.getInt("errorCode");
            if (code == 0) {
                JSONObject data = response.getJSONObject("data");
                JSONArray record = data.getJSONArray("rooms");
                Type listType = new TypeToken<ArrayList<RoomInfoJson>>() {
                }.getType();
                roomList = new Gson().fromJson(record.toString(), listType);
            }
            String errorInfo = response.getString("errorInfo");
            return new RequestBackInfo(code, errorInfo);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 通知UserServer结束房间
     */
    public RequestBackInfo notifyCloseLive() {
        try {
            JSONObject jasonPacket = new JSONObject();
            jasonPacket.put("token", MySelfInfo.getInstance().getToken());
            jasonPacket.put("roomnum", MySelfInfo.getInstance().getMyRoomNum());
            jasonPacket.put("type", "live");
            String json = jasonPacket.toString();
            String res = post(STOP_ILIVE, json);
            JSONTokener jsonParser = new JSONTokener(res);
            JSONObject response = (JSONObject) jsonParser.nextValue();
            int code = response.getInt("errorCode");
            String errorInfo = response.getString("errorInfo");
            return new RequestBackInfo(code, errorInfo);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 上报成员
     */
    public RequestBackInfo reportMe(int role, int action) {
        try {
            JSONObject jasonPacket = new JSONObject();

            jasonPacket.put("token", MySelfInfo.getInstance().getToken());
            jasonPacket.put("roomnum", CurLiveInfo.getRoomNum());
            jasonPacket.put("id", MySelfInfo.getInstance().getId());
            jasonPacket.put("role", role);
            jasonPacket.put("operate", action);

            String json = jasonPacket.toString();
            String res = post(REPORT_ME, json);
            ILiveLog.i(TAG, "reportMe " + role + " action " + action);
            JSONTokener jsonParser = new JSONTokener(res);
            JSONObject response = (JSONObject) jsonParser.nextValue();
            int code = response.getInt("errorCode");
            String errorInfo = response.getString("errorInfo");
            return new RequestBackInfo(code, errorInfo);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 获取房间内成员
     */
    public ArrayList<MemberID> getMemberList() {
        try {
            JSONObject jasonPacket = new JSONObject();
            jasonPacket.put("token", MySelfInfo.getInstance().getToken());
            jasonPacket.put("roomnum", CurLiveInfo.getRoomNum());
            jasonPacket.put("index", 0);
            jasonPacket.put("size", 40);

            String json = jasonPacket.toString();
            String res = post(GET_MEMLIST, json);
            JSONTokener jsonParser = new JSONTokener(res);
            JSONObject response = (JSONObject) jsonParser.nextValue();
            int code = response.getInt("errorCode");
            String errorInfo = response.getString("errorInfo");
            if (code == 0) {
                JSONObject data = response.getJSONObject("data");
                JSONArray record = data.getJSONArray("idlist");
                Type listType = new TypeToken<ArrayList<MemberID>>() {
                }.getType();
                ArrayList<MemberID> result = new Gson().fromJson(record.toString(), listType);
                ILiveLog.i(TAG, "size" + result.size());
                return result;
            }
            return null;
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    //
    //    /**
    //     * 上报录制视频URL
    //     */
    //    public RequestBackInfo reportRecord(String videoid,String videoUrl,int type,String cover) {
    //        try {
    //            JSONObject jasonPacket = new JSONObject();
    //            jasonPacket.put("token", MySelfInfo.getInstance().getToken());
    //            jasonPacket.put("videoid", videoid);
    //            jasonPacket.put("playurl",videoUrl);
    //            jasonPacket.put("type", type);
    //            jasonPacket.put("cover",cover);
    //
    //            String json = jasonPacket.toString();
    //            String res = post(REPORT_RECORD, json);
    //            JSONTokener jsonParser = new JSONTokener(res);
    //            JSONObject response = (JSONObject) jsonParser.nextValue();
    //            int code = response.getInt("errorCode");
    //            String errorInfo = response.getString("errorInfo");
    //            return new RequestBackInfo(code, errorInfo);
    //        } catch (JSONException e) {
    //            e.printStackTrace();
    //        } catch (IOException e) {
    //            e.printStackTrace();
    //        }
    //        return null;
    //    }
    //
    //
    //
    //
    //
    //    /**
    //     * 获取点播列表
    //     */
    //    public RequestBackInfo getRecordList() {
    //        try {
    //            JSONObject jasonPacket = new JSONObject();
    //            jasonPacket.put("token", MySelfInfo.getInstance().getToken());
    //            jasonPacket.put("type", 0);
    //            jasonPacket.put("index",0);
    //            jasonPacket.put("size", 10);
    //            String json = jasonPacket.toString();
    //            String res = post(GET_REOCORDLIST, json);
    //            JSONTokener jsonParser = new JSONTokener(res);
    //            JSONObject response = (JSONObject) jsonParser.nextValue();
    //            int code = response.getInt("errorCode");
    //            String errorInfo = response.getString("errorInfo");
    //            return new RequestBackInfo(code, errorInfo);
    //        } catch (JSONException e) {
    //            e.printStackTrace();
    //        } catch (IOException e) {
    //            e.printStackTrace();
    //        }
    //        return null;
    //    }


    /**
     * 拉取录制列表
     */
    public ArrayList<RecordInfo> getRecordList(int page, int size) {
        try {
            JSONObject jasonPacket = new JSONObject();
            jasonPacket.put("token", MySelfInfo.getInstance().getToken());
            jasonPacket.put("type", Constants.VOD_MODE);
            jasonPacket.put("index", page);
            jasonPacket.put("size", size);
            String json = jasonPacket.toString();
            Log.v(TAG, "getRecordList->request: " + json);
            String res = post(GET_REOCORDLIST, json);
            Log.v(TAG, "getRecordList->ret: " + res);
            JSONTokener jsonParser = new JSONTokener(res);
            JSONObject response = (JSONObject) jsonParser.nextValue();
            int code = response.getInt("errorCode");
            String errorInfo = response.getString("errorInfo");
            if (code == 0) {
                JSONObject data = response.getJSONObject("data");
                JSONArray record = data.getJSONArray("videos");
                ArrayList<RecordInfo> recList = new ArrayList<>();
                for (int i = 0; i < record.length(); i++) {
                    recList.add(new RecordInfo(record.getJSONObject(i)));
                }
                ILiveLog.i(TAG, "size" + recList.size());
                return recList;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取播放列表
     */
    public RequestBackInfo getPlayUrlList(int page, int size) {
        try {
            JSONObject jasonPacket = new JSONObject();
            jasonPacket.put("token", MySelfInfo.getInstance().getToken());
            jasonPacket.put("index", page);
            jasonPacket.put("size", size);
            String json = jasonPacket.toString();
            String res = post(GET_PLAYERLIST, json);
            JSONTokener jsonParser = new JSONTokener(res);
            JSONObject response = (JSONObject) jsonParser.nextValue();
            int code = response.getInt("errorCode");
            String errorInfo = response.getString("errorInfo");
            return new RequestBackInfo(code, errorInfo);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 获取房间回放地址
     */
    public RequestBackInfo getRoomPlayUrl(int room) {
        try {
            JSONObject jasonPacket = new JSONObject();
            jasonPacket.put("token", MySelfInfo.getInstance().getToken());
            jasonPacket.put("roomnum", room);
            String json = jasonPacket.toString();
            String res = post(GET_ROOM_PLAYURL, json);
            JSONTokener jsonParser = new JSONTokener(res);
            JSONObject response = (JSONObject) jsonParser.nextValue();
            int code = response.getInt("errorCode");
            if (code == 0) {
                JSONObject data = response.getJSONObject("data");
                String address = data.getString("address");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    public String getCosSig() {
        try {
            String response = UserServerHelper.getInstance().post(GET_COS_SIG, "");
            JSONTokener jsonParser = new JSONTokener(response);
            JSONObject reg_response = (JSONObject) jsonParser.nextValue();
            int ret = reg_response.getInt("errorCode");
            if (ret == 0) {
                JSONObject data = reg_response.getJSONObject("data");
                String sign = data.getString("sign");
                return sign;
            }

        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


}
